アプリ開発コース 2
◆物理演算
Step 1. 円を描く
code:cpp
# include <Siv3D.hpp>
void Main()
{
while (System::Update())
{
Circle(0, 0, 5).draw();
}
}
Step 2. 2D カメラを使う
code:cpp
# include <Siv3D.hpp>
# include <HamFramework.hpp> // Cmaera2D 用
void Main()
{
// 中心 (0, 0), 倍率 20 倍のカメラ
Camera2D camera(Vec2(0, 0), 20);
while (System::Update())
{
// カメラの更新(必ず必要)
camera.update();
// カメラの適用
auto cameraTransform = camera.createTransformer();
Circle(0, 0, 5).draw();
// 補助 UI の描画
camera.draw(Palette::Orange);
}
}
Step 3. Line を設置する
code:cpp
# include <Siv3D.hpp>
# include <HamFramework.hpp>
void Main()
{
// 物理演算用の世界
// 重力 (0, 9.8)
P2World world(Vec2(0, 9.8));
// 物理世界に Line を作る
// P2Material(密度, 反発のしやすさ, 摩擦)
const P2Body line = world.createLine(Vec2(0, 0), Line(-5, 0, 5, 1), P2Material(1.0, 0.1, 0.2));
Camera2D camera(Vec2(0, 0), 20);
while (System::Update())
{
// 物理世界の更新
// イテレーション数 20, 10 (大きいほど精度が高い)
world.update(System::DeltaTime(0.05), 20, 10);
camera.update();
auto cameraTransform = camera.createTransformer();
// line を描画
line.draw();
camera.draw(Palette::Orange);
}
}
Step 4. 線を固定する
code:cpp
# include <Siv3D.hpp>
# include <HamFramework.hpp>
void Main()
{
P2World world(Vec2(0, 9.8));
// Static 物体にする
const P2Body line = world.createLine(Vec2(0, 0), Line(-5, 0, 5, 1), P2Material(1.0, 0.1, 0.2), none, P2BodyType::Static);
Camera2D camera(Vec2(0, 0), 20);
while (System::Update())
{
world.update(System::DeltaTime(0.05), 20, 10);
camera.update();
auto cameraTransform = camera.createTransformer();
line.draw();
camera.draw(Palette::Orange);
}
}
Step 5. ボールを落とす
code:cpp
# include <Siv3D.hpp>
# include <HamFramework.hpp>
void Main()
{
P2World world(Vec2(0, 9.8));
const P2Body line = world.createLine(Vec2(0, 0), Line(-5, 0, 5, 1), P2Material(1.0, 0.1, 0.2), none, P2BodyType::Static);
// 物理世界にボールを追加
const P2Body ball = world.createCircle(Vec2(0, -8), 0.5, P2Material(1.0, 0.1, 0.2));
Camera2D camera(Vec2(0, 0), 20);
while (System::Update())
{
world.update(System::DeltaTime(0.05), 20, 10);
camera.update();
auto cameraTransform = camera.createTransformer();
line.draw();
// ボールを描画
ball.draw(Palette::Skyblue);
camera.draw(Palette::Orange);
}
}
Step 6. LineString を作る
code:cpp
# include <Siv3D.hpp>
# include <HamFramework.hpp>
void Main()
{
P2World world(Vec2(0, 9.8));
// LineString を作る
const P2Body line = world.createLineString(Vec2(0, 0), LineString{ Vec2(-5, 0), Vec2(5, 1), Vec2(5, 0) }, P2Material(1.0, 0.1, 0.2), none, P2BodyType::Static);
const P2Body ball = world.createCircle(Vec2(0, -8), 0.5, P2Material(1.0, 0.1, 0.2));
Camera2D camera(Vec2(0, 0), 20);
while (System::Update())
{
world.update(System::DeltaTime(0.05), 20, 10);
camera.update();
auto cameraTransform = camera.createTransformer();
line.draw();
ball.draw(Palette::Skyblue);
camera.draw(Palette::Orange);
}
}
Step 7. ボールを作る
code:cpp
# include <Siv3D.hpp>
# include <HamFramework.hpp>
void Main()
{
P2World world(Vec2(0, 9.8));
const P2Body line = world.createLineString(Vec2(0, 0), LineString{ Vec2(-5, 0), Vec2(5, 1), Vec2(5, 0) }, P2Material(1.0, 0.1, 0.2), none, P2BodyType::Static);
// ボールの配列
Array<P2Body> balls;
Camera2D camera(Vec2(0, 0), 20);
while (System::Update())
{
world.update(System::DeltaTime(0.05), 20, 10);
camera.update();
auto cameraTransform = camera.createTransformer();
// 左クリックされたら
if (MouseL.down())
{
// ボールを追加
// 座標が (0.3, -0.5) みたいに Vec2 型になるので Cursor::PosF()
balls << world.createCircle(Cursor::PosF(), 0.5, P2Material(1.0, 0.1, 0.2));
}
line.draw();
// すべてのボールを描画
for (const auto& ball : balls)
{
ball.draw(Palette::Skyblue);
}
camera.draw(Palette::Orange);
}
}
Step 8. 応用
world.createRect で長方形を作る
world.createPolygon で Polygon (Shape2D) を作る
足場を増やす
P2Material の設定を変えてみる